/**
 * TabletBrainWaveRequest 数据模型测试
 * 
 * 用途：验证新的 TabletBrainWaveRequest 数据模型的正确性和JSON序列化
 * 方法：使用JUnit进行单元测试，验证数据结构和序列化功能
 * 原理：确保数据模型符合服务端API要求，字段完整且类型正确
 */

import com.example.brain_wave.api.model.TabletBrainWaveRequest
import com.google.gson.Gson
import org.junit.Test
import org.junit.Assert.*

class TabletBrainWaveRequestTest {

    private val gson = Gson()

    @Test
    fun testTabletBrainWaveRequestCreation() {
        // 创建测试数据
        val rawValues = (1..512).map { it * 10 } // 模拟512Hz数据
        val request = TabletBrainWaveRequest(
            session_id = "test_session_123",
            room_id = "room_456",
            timestamp = System.currentTimeMillis(),
            raw_value = rawValues,
            delta = 1000,
            theta = 800,
            low_alpha = 600,
            high_alpha = 700,
            low_beta = 500,
            high_beta = 400,
            low_gamma = 300,
            mid_gamma = 200,
            attention = 75,
            meditation = 60,
            poor_signal = 15,
            blink_strength = 128
        )

        // 验证基本字段
        assertEquals("test_session_123", request.session_id)
        assertEquals("room_456", request.room_id)
        assertEquals(512, request.raw_value.size)
        
        // 验证频段数据
        assertEquals(1000, request.delta)
        assertEquals(800, request.theta)
        assertEquals(600, request.low_alpha)
        assertEquals(700, request.high_alpha)
        assertEquals(500, request.low_beta)
        assertEquals(400, request.high_beta)
        assertEquals(300, request.low_gamma)
        assertEquals(200, request.mid_gamma)
        
        // 验证NeuroSky算法输出
        assertEquals(75, request.attention)
        assertEquals(60, request.meditation)
        
        // 验证信号质量指标
        assertEquals(15, request.poor_signal)
        assertEquals(128, request.blink_strength)
    }

    @Test
    fun testJsonSerialization() {
        // 创建测试数据
        val rawValues = listOf(100, 200, 300, 400, 500) // 简化的原始数据
        val request = TabletBrainWaveRequest(
            session_id = "json_test_session",
            room_id = "json_test_room",
            timestamp = 1640995200000L, // 固定时间戳便于测试
            raw_value = rawValues,
            delta = 1500,
            theta = 1200,
            low_alpha = 900,
            high_alpha = 1000,
            low_beta = 800,
            high_beta = 600,
            low_gamma = 400,
            mid_gamma = 300,
            attention = 85,
            meditation = 70,
            poor_signal = 10,
            blink_strength = 64
        )

        // 序列化为JSON
        val json = gson.toJson(request)
        assertNotNull(json)
        assertTrue(json.contains("\"session_id\":\"json_test_session\""))
        assertTrue(json.contains("\"room_id\":\"json_test_room\""))
        assertTrue(json.contains("\"timestamp\":1640995200000"))
        assertTrue(json.contains("\"raw_value\":[100,200,300,400,500]"))
        assertTrue(json.contains("\"delta\":1500"))
        assertTrue(json.contains("\"attention\":85"))
        assertTrue(json.contains("\"poor_signal\":10"))

        // 反序列化验证
        val deserializedRequest = gson.fromJson(json, TabletBrainWaveRequest::class.java)
        assertEquals(request.session_id, deserializedRequest.session_id)
        assertEquals(request.room_id, deserializedRequest.room_id)
        assertEquals(request.timestamp, deserializedRequest.timestamp)
        assertEquals(request.raw_value, deserializedRequest.raw_value)
        assertEquals(request.delta, deserializedRequest.delta)
        assertEquals(request.attention, deserializedRequest.attention)
        assertEquals(request.poor_signal, deserializedRequest.poor_signal)
    }

    @Test
    fun testRawValueSize() {
        // 测试512Hz采样数据大小
        val rawValues512 = (1..512).map { kotlin.random.Random.nextInt(-2048, 2048) }
        val request = TabletBrainWaveRequest(
            session_id = "size_test",
            room_id = "test_room",
            timestamp = System.currentTimeMillis(),
            raw_value = rawValues512,
            delta = 1000, theta = 800, low_alpha = 600, high_alpha = 700,
            low_beta = 500, high_beta = 400, low_gamma = 300, mid_gamma = 200,
            attention = 50, meditation = 50, poor_signal = 20, blink_strength = 0
        )

        assertEquals(512, request.raw_value.size)
        
        // 验证数据范围（12位ADC: -2048 到 2047）
        request.raw_value.forEach { value ->
            assertTrue("Raw value $value should be in range [-2048, 2047]", 
                      value >= -2048 && value <= 2047)
        }
    }

    @Test
    fun testFrequencyBandValues() {
        // 测试频段数据的合理性
        val request = TabletBrainWaveRequest(
            session_id = "freq_test",
            room_id = "test_room",
            timestamp = System.currentTimeMillis(),
            raw_value = emptyList(),
            delta = 2000,      // Delta波通常较高
            theta = 1500,      // Theta波中等
            low_alpha = 1000,  // Alpha波分为两部分
            high_alpha = 1200,
            low_beta = 800,    // Beta波分为两部分
            high_beta = 600,
            low_gamma = 400,   // Gamma波通常较低
            mid_gamma = 300,
            attention = 75,    // 专注度 0-100
            meditation = 60,   // 冥想度 0-100
            poor_signal = 15,  // 信号质量 0-200（越低越好）
            blink_strength = 128 // 眨眼强度 0-255
        )

        // 验证NeuroSky算法输出范围
        assertTrue("Attention should be 0-100", request.attention in 0..100)
        assertTrue("Meditation should be 0-100", request.meditation in 0..100)
        
        // 验证信号质量范围
        assertTrue("Poor signal should be 0-200", request.poor_signal in 0..200)
        
        // 验证眨眼强度范围
        assertTrue("Blink strength should be 0-255", request.blink_strength in 0..255)
        
        // 验证频段数据为正值
        assertTrue("Delta should be positive", request.delta >= 0)
        assertTrue("Theta should be positive", request.theta >= 0)
        assertTrue("Low Alpha should be positive", request.low_alpha >= 0)
        assertTrue("High Alpha should be positive", request.high_alpha >= 0)
        assertTrue("Low Beta should be positive", request.low_beta >= 0)
        assertTrue("High Beta should be positive", request.high_beta >= 0)
        assertTrue("Low Gamma should be positive", request.low_gamma >= 0)
        assertTrue("Mid Gamma should be positive", request.mid_gamma >= 0)
    }

    @Test
    fun testEmptyRawValueHandling() {
        // 测试空原始数据的处理
        val request = TabletBrainWaveRequest(
            session_id = "empty_raw_test",
            room_id = "test_room",
            timestamp = System.currentTimeMillis(),
            raw_value = emptyList(), // 空的原始数据
            delta = 1000, theta = 800, low_alpha = 600, high_alpha = 700,
            low_beta = 500, high_beta = 400, low_gamma = 300, mid_gamma = 200,
            attention = 50, meditation = 50, poor_signal = 20, blink_strength = 0
        )

        assertEquals(0, request.raw_value.size)
        
        // JSON序列化应该正常工作
        val json = gson.toJson(request)
        assertTrue(json.contains("\"raw_value\":[]"))
        
        // 反序列化应该正常工作
        val deserializedRequest = gson.fromJson(json, TabletBrainWaveRequest::class.java)
        assertEquals(0, deserializedRequest.raw_value.size)
    }
}